package com.dtsz.m2.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.MainActivity;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.jpushdemo.ExampleUtil;
import com.dtsz.m2.jpushdemo.TagAliasOperatorHelper;
import com.dtsz.m2.jpushdemo.intrefase.JpushResultInterface;
import com.dtsz.m2.provider.DataUtils;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.RegisterVo;
import com.dtsz.m2.ui.model.Users;
import com.dtsz.m2.utils.IMEIUtils;
import com.dtsz.m2.utils.RSAEncrypt;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.volley.state.HUCState;

import java.util.LinkedHashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

import static com.dtsz.m2.jpushdemo.TagAliasOperatorHelper.sequence;

public class LoginActivity extends BaseActivity implements JpushResultInterface {
    private EditText etName;
    private EditText etPassword;
    private Button buPwd;
    private RegisterVo registerVo = new RegisterVo();

    private TextView tvPwd, tvReg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
        super.initView();
        etName = findViewById(R.id.et_name);
        etPassword = findViewById(R.id.et_password);
        buPwd = findViewById(R.id.bt_pwd);
        tvPwd = findViewById(R.id.tv_pwd);
        tvReg = findViewById(R.id.tv_reg);
    }

    @Override
    public void initClick() {
        super.initClick();
        buPwd.setOnClickListener(this);
        tvPwd.setOnClickListener(this);
        tvReg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_pwd:
                String phoneStr = etName.getText().toString();
                if (StringUtils.isEmpty(phoneStr)) {
                    Toast.makeText(this, "登录账号不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                String pwdStr = etPassword.getText().toString();
                if (StringUtils.isEmpty(pwdStr)) {
                    Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
                }
                loading();
                registerVo.setPhone(phoneStr);
                registerVo.setPassword(RSAEncrypt.encrypt(pwdStr));
                registerVo.setClientId(JPushInterface.getRegistrationID(getApplicationContext()));
                hucRequestInterfase.executeStr(LoginActivity.this, Users.class,
                        HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.LOGIN_URL,
                        hucParameInterfase.getRequestParameter(registerVo), true);
                break;
            case R.id.tv_pwd:
                sendIntent(this, RetrievePwdActivity.class);
                break;
            case R.id.tv_reg:
                sendIntent(this, RegisterActivity.class);
                break;
        }
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (response instanceof Users) {
                    Users users = (Users) response;
                    setSharedPreferences("tokenRSA", users.getBuTokenM2());
                    setSharedPreferences("phone", users.getBuPhone());
                    setSharedPreferences("card", users.getBuCard());
                    setSharedPreferences("name", users.getBuName());
                    setSharedPreferences("buCertification", users.getBuCertification());
                    finish();
                    Intent intent = new Intent();
                    intent.setClass(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
                }
                break;
        }
        loadDismiss();
    }

    @Override
    public void onSuccessfJpushResult(String strName) {
        showToast("tag 绑定成功" + strName);
    }
}
